package com.beaven.imclient.base;

import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

/**
 * author: Beaven
 * time: 2017/6/8 17:13
 */

public abstract class BaseHideActivity extends BaseActivity {
  @Override public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      View view = getCurrentFocus();
      if (isShouldHideInput(view, ev)) {
        hideSoftInput(view.getWindowToken());
      }
    }
    return super.dispatchTouchEvent(ev);
  }

  // 判断edit是否需要隐藏软键盘
  private boolean isShouldHideInput(View view, MotionEvent motionEvent) {
    if (view != null && (view instanceof EditText)) {
      int[] l = { 0, 0 };
      view.getLocationInWindow(l);
      int left = l[0];
      int top = l[1];
      int bottom = top + view.getHeight();
      int right = left + view.getWidth();
      if (motionEvent.getX() > left
          && motionEvent.getX() < right
          && motionEvent.getY() > top
          && motionEvent.getY() < bottom) {
        return false;
      } else {
        return true;
      }
    }
    return false;
  }
}
